import Vue from 'vue'
import VueRouter from 'vue-router'

// 导入管理后台全局样式
import '../AdminApp.vue'

// 管理员布局和页面组件
import AdminLogin from '../views/Login.vue'
import AdminDashboard from '../views/Dashboard.vue'
import AdminChallenges from '../views/Challenges.vue'
import AdminChallengeEdit from '../views/ChallengeEdit.vue'
import AdminUsers from '../views/Users.vue'
import AdminUserEdit from '../views/UserEdit.vue'
import AdminSettings from '../views/Settings.vue'
import AdminLogs from '../views/Logs.vue'
import AdminArticles from '../views/Articles.vue'
import AdminArticleEdit from '../views/ArticleEdit.vue'
import AdminPoints from '../views/Points.vue'
import AdminPointEdit from '../views/PointEdit.vue'
import AdminFeedback from '../views/Feedback.vue'
import AdminFeedbackDetail from '../views/FeedbackDetail.vue'

// 404页面
import NotFound from '../views/NotFound.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: AdminLogin,
    meta: { requiresAuth: false }
  },
  {
    path: '/admin',
    redirect: '/admin/dashboard',
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/dashboard',
    name: 'AdminDashboard',
    component: AdminDashboard,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/challenges',
    name: 'AdminChallenges',
    component: AdminChallenges,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/challenges/new',
    name: 'AdminChallengeCreate',
    component: AdminChallengeEdit,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/challenges/:id',
    name: 'AdminChallengeEdit',
    component: AdminChallengeEdit,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/users',
    name: 'AdminUsers',
    component: AdminUsers,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/users/:id',
    name: 'AdminUserEdit',
    component: AdminUserEdit,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/settings',
    name: 'AdminSettings',
    component: AdminSettings,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/logs',
    name: 'AdminLogs',
    component: AdminLogs,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/articles',
    name: 'AdminArticles',
    component: AdminArticles,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/articles/new',
    name: 'AdminArticleCreate',
    component: AdminArticleEdit,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/articles/:id',
    name: 'AdminArticleEdit',
    component: AdminArticleEdit,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/points',
    name: 'AdminPoints',
    component: AdminPoints,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/points/new',
    name: 'AdminPointCreate',
    component: AdminPointEdit,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/points/:id',
    name: 'AdminPointEdit',
    component: AdminPointEdit,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/feedback',
    name: 'AdminFeedback',
    component: AdminFeedback,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/feedback/:id',
    name: 'AdminFeedbackDetail',
    component: AdminFeedbackDetail,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/*',
    name: 'AdminNotFound',
    component: NotFound,
    meta: { requiresAuth: true }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 导航守卫，处理管理员权限验证
router.beforeEach((to, from, next) => {
  // 注意：这里需要修改判断逻辑，因为我们可能在应用内导航到其他非admin路由
  if (!to.path.startsWith('/admin')) {
    next()
    return
  }
  
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
  const isLoggedIn = localStorage.getItem('adminToken') // 假设管理员token存储在localStorage
  
  if (requiresAuth && !isLoggedIn) {
    next('/admin/login')
  } else {
    next()
  }
})

// 导出路由实例和路由配置
export { routes }
export default router 